package com.henry.imkit

import com.alibaba.fastjson.JSON
import com.henry.imkit.models.Conversation
import com.henry.imkit.models.ConversationData
import com.henry.imkit.models.IMChatNotice
import com.henry.imkit.models.MessageData
import com.henry.imkit.request.IMRequest
import com.henry.imkit.utils.IMLog

object IMConvertUtils {

    fun convertConversation(data: ConversationData): Conversation {
        val conversation = Conversation()
        conversation.id = data.eacgid.toString()
        conversation.type = data.eacgtype.toString()
        conversation.name = data.eacgname.toString()
        conversation.icon = data.eacgheadportrait.toString()
        conversation.top = data.eacgmistop
        conversation.unRead = data.unreadtotals
        conversation.lastMessageDisplay = data.eacglastmsg.toString()
        conversation.lastMessageTime = data.eacglastmsgtime
        return conversation
    }

    fun convertConversations(list: List<ConversationData>): List<Conversation> {
        val array = mutableListOf<Conversation>()
        for (data in list) {
            array.add(convertConversation(data))
        }
        return array
    }

    fun convertConversationData(data: IMChatNotice): ConversationData {
        val conversation = ConversationData()
        conversation.eacgid = data.eacgid
        conversation.eacgtype = data.eacgtype
        conversation.eacgname = data.eacgname
        conversation.eacgheadportrait = data.eacgheadportrait
        conversation.eacgmistop = data.eacgmistop
        conversation.unreadtotals = data.unreadtotals
        conversation.eacglastmsg = data.eacglastmsg
        conversation.eacglastmsgtime = data.eacglastmsgtime
        conversation.eacgexpandid = data.eacgexpandid
        return conversation
    }

    fun convertConversation(data: IMChatNotice): Conversation {
        val conversation = Conversation()
        conversation.id = data.eacgid.toString()
        conversation.type = data.eacgtype
        conversation.name = data.eacgname
        conversation.icon = data.eacgheadportrait
        conversation.top = data.eacgmistop
        conversation.unRead = data.unreadtotals
        conversation.lastMessageDisplay = data.eacglastmsg.toString()
        conversation.lastMessageTime = data.eacglastmsgtime
        return conversation
    }

    fun convertMessages(list: List<MessageData>): List<IMMessage> {
        val array = mutableListOf<IMMessage>()
        for (data in list) {
            array.add(convertMessage(data))
        }
        return array
    }

    fun convertMessage(data: MessageData): IMMessage {
        val msg = IMMessage()
        msg.eamid = data.eamid
        msg.eamtimestamp = data.eamtimestamp
        msg.eamfromid = data.eamfromid
        msg.eamfromname = data.eamfromname
        msg.eamfromheadportrait = data.eamfromheadportrait
        msg.eamtoid = data.eamtoid
        msg.eamseq = data.eamseq
        msg.eamrandom = data.eamrandom
        msg.eamessagetype = data.eamessagetype
        msg.eamreadlogtime = data.eamreadlogtime

        val array = JSON.parseArray(data.eamessagecontent, Map::class.java)

        for (item in array) {
            when (item["type"]) {
                "TIMTextElem" -> msg.addElem(JSON.parseObject(JSON.toJSONString(item), IMTextElem::class.java))
                "TIMImageElem" -> {
                    val elem = JSON.parseObject(JSON.toJSONString(item), IMImageElem::class.java)
                    for (image in elem.imagearray!!) {
                        image.url = IMRequest.getCompleteUrl(image.url)
                    }
                    msg.addElem(elem)
                }
                "TIMSoundElem" -> {
                    val elem = JSON.parseObject(JSON.toJSONString(item), IMSoundElem::class.java)
                    elem.url = IMRequest.getCompleteUrl(elem.url)
                    IMLog("TIMSoundElem : " + elem.url + "  " + elem.duration)
                    msg.addElem(elem)
                }
                "TIMRevokeElem" -> msg.addElem(JSON.parseObject(JSON.toJSONString(item), IMRevokeElem::class.java))
                "TIMSystemElem" -> msg.addElem(JSON.parseObject(JSON.toJSONString(item), IMSystemElem::class.java))
                "TIMCustomElem" -> {
                    val elem = IMCustomElem()
                    elem.data = JSON.toJSONString(item)
                    msg.addElem(elem)
                }
            }
        }
        return msg
    }

    fun convertIMMessage(send: IMSendMessage): IMMessage {
        val msg = IMMessage()
        msg.eamrandom = send.eamrandom
        return msg
    }

}